#! /usr/bin/env python2  
# encoding=utf-8
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
import os
ScriptPath = os.path.split( os.path.realpath( sys.argv[0] ) )[0]
import xmlrpclib
import ConfigParser
import datetime

# 维保合同到期 执行中 过期 审核通过的 ==>电梯脱保
# 状态：('new', '新建'), ('execute', '执行'), ('due', '过期')
# 审核状态：('pending', '待审核'), ('approved', '审核通过'), ('reject', '审核未通过')
config = ConfigParser.RawConfigParser()
config.read(ScriptPath+'/sys.cfg')
url = config.get('odoo','host')
uid = 1
pwd = config.get('odoo','password')
db = config.get('odoo','db')
models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))

def getToday():
    date = datetime.datetime.now().strftime('%Y-%m-%d')
    return date

def main():
    print u"开始更新维保合同到期"
    main_contract = models.execute_kw(db,uid,pwd,'tyibs.main.contract','search_read',
        [[['status','=','execute'],['apply_status','=','approved'],['end','<',getToday()]]],
        {'fields':['maintenance_company_id','end','status','apply_status','lift_ids']}
    )
    con_ids=[]
    for main_c in main_contract:
        con_ids.append(main_c['id'])
    if len(con_ids) != 0:
        print u"有更新维保合同"
        models.execute_kw(db,uid,pwd,'tyibs.main.contract','past_due',[con_ids])
    else:
        print u"无更新维保合同"


if __name__ == '__main__':
    main()